import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

class IdfaHelper {
  static const TAG = "IdfaHelper";

  static const MethodChannel _channel = const MethodChannel('idfa_helper');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  initHelper(IdfaListener listener,
      {String certFilePath = "", bool showLog = true}) {
    AndroidHelper(_channel, certFilePath, showLog);
    iosHelper(_channel);
    _channel.setMethodCallHandler((call) {
      switch (call.method) {
        case "onIdfaValid":
          print("$TAG,${call.arguments}");
          listener.onIdfaValid.call(call.arguments.toString());
          break;
        case "error":
          print("$TAG,error : ${call.arguments}");
          listener.error.call(call.arguments.toString());
          break;
        default:
          break;
      }
      return;
    });
  }
}

class IdfaListener {
  ValueChanged<String> error;
  ValueChanged<String> onIdfaValid;
}

class AndroidHelper {
  MethodChannel _channel;
  String certFilePath;
  bool showLog;

  AndroidHelper(this._channel, this.certFilePath, this.showLog) {
    initSDK();
  }

  void initSDK() async {
    await _channel.invokeMethod("initAndroid");
    await _channel
        .invokeMethod("startGetId", {"certFilePath": "", "": "openLog"});
  }
}

class iosHelper {
  MethodChannel _channel;

  iosHelper(this._channel);
}
